#define _CRT_SECURE_NO_WARNINGS 1
#include"seqlist.h"

void SLInit(SL* psl)
{
	/*psl->a = NULL;
	psl->sz = 0;
	psl->capacity = 0;*/

	psl->a = (SLDataType*)malloc(sizeof(SLDataType)*4);
	if (psl->a==NULL)
	{
		perror("malloc fail!");
		return;
	}
	psl->sz = 0;
	psl->capacity = 4;
}

void SLDestory(SL* psl)
{
	assert(psl);
	free(psl->a);
	psl->a = NULL;
	psl->sz = 0;
	psl->capacity = 0;
}


void SLCheckCapcity(SL* psl)
{
	if (psl->sz == psl->capacity)
	{
		SLDataType* temp = (SLDataType*)realloc(psl->a, sizeof(SLDataType) * psl->capacity * 2);
		if (temp==NULL)
		{
			perror("malloc fail");
		}
		psl->a = temp;
		psl->capacity *= 2;
	}
}

void SLPrint(SL* psl)
{
	int i = 0;
	for (i = 0; i < psl->sz; i++)
	{
		printf("%d", psl->a[i]);
	}
	printf("\n");
}

void SLPushFront(SL* psl, SLDataType x)
{
	SLCheckCapcity(psl);
	int end = psl->sz - 1;
	while (end>=0)
	{
		psl->a[end + 1] = psl->a[end];
		end--;
	}
	psl->a[0] = x;
	psl->sz++;
	/*SLInsert(psl, 0, x);*/
}

void SLPushBack(SL* psl, SLDataType x)
{
	SLCheckCapcity(psl);
	psl->a[psl->sz] = x;
	psl->sz++;
	/*SLInsert(psl, psl->sz, x);*/
}

void SLPopFront(SL* psl)
{
	if (psl->sz == 0)
	{
		return;
	}
	assert(psl);
	int start = 0;
	while (start < psl->sz)
	{
		psl->a[start] = psl->a[start + 1];
		start++;
	}
	psl->sz--;
	/*SLEase(psl, 0);*/
}

void SLPopBack(SL* psl)
{
	assert(psl);
	psl->sz--;
	/*SLEase(psl, psl->sz-1);*/
}

void SLInsert(SL* psl, int pos,SLDataType x)
{
	assert(pos >= 0 && pos <= psl->sz);
	SLCheckCapcity(psl);
	int end = psl->sz - 1;
	while (end >= pos)
	{
		psl->a[pos + 1] = psl->a[pos];
		end--;
	}
	psl->a[pos] = x;
	psl->sz++;
}

void SLEase(SL* psl,int pos)
{
	assert(pos >= 0 && pos <= psl->sz);
	assert(psl);
	int start = pos+1;
	while (start < psl->sz)
	{
		psl->a[start - 1] = psl->a[start];
		start++;
	}
	psl->sz--;

}

void SLFind(SL* psl, SLDataType x)
{
	for (int i = 0; i < psl->sz; i++)
	{
		if (psl->a[i] == x)
		{
			return i;
		}
	}
	return -1;
}

void SLModify(SL* psl,int pos, SLDataType x)
{
	assert(psl);
	assert(pos >= 0 && pos <= psl->sz);
	psl->a[pos] = x;
}